package ioTest;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {


    public static void main(String[] args) throws IOException {
        //从网络中爬取数据
        //1.定义变量记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
        //2.调用方法获取数据并将数据进行打印
        String familyStr = webCrawler(familyNameNet);
        String boyNameStr = webCrawler(boyNameNet);
        String girlNameStr = webCrawler(girlNameNet);
       //System.out.println(familyStr);
        //System.out.println(boyNameStr);
         //System.out.println(girlNameStr);/^[\u4E00-\u9FA5]+$/
         //3.通过正则进行获取
        ArrayList<String> familyTempList = getData(familyStr, "(.{4})(，|。)",1);
        //System.out.println(familyTempList);
        ArrayList<String> boyTempList = getData(boyNameStr, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
       //System.out.println(boyTempList);
        ArrayList<String> girlTempList = getData(girlNameStr, "(.. ){4}..", 0);
        //System.out.println(girlTempList);
        //将临时数据进行处理
        //姓氏
        ArrayList<String> familyNameList = new ArrayList<>();
        ArrayList<String> girlNameList = new ArrayList<>();
        ArrayList<String> boyNameList = new ArrayList<>();
        //将姓氏进行切割
        for (String s : familyTempList) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                //这里进行拼接形成字符串
                familyNameList.add(c + "");
            }

        }
        //System.out.println(familyNameList);

        //对男生名字进行整理 ,主要为去重
        for (String s : boyTempList) {
            if(!boyNameList.contains(s)){
                boyNameList.add(s);
            }
        }
        //System.out.println(boyNameList);

        //对女生到名字进行切割
        for (String s : girlTempList) {
            String[] arr = s.split(" ");
            for (String str : arr) {
                girlNameList.add(str);
            }
        }
        //System.out.println(girlNameList);
        ArrayList<String> list = getInfos(familyNameList, boyNameList, girlNameList, 60, 40);
        System.out.println(list);
        //添加到本地文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter("IODemo\\src\\ioTest\\names.txt"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();


    }
    //
    public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList,int boyCount,int girlCount){
        //1.根据男生的数量进行信息的生成
        HashSet<String> boyhs = new HashSet<>();
        //2.通过循环添加数据
        while(true){
            if(boyhs.size() == boyCount){
                break;
            }
            //3.打乱集合顺序
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyhs.add(familyNameList.get(0) + boyNameList.get(0));
        }
        //3.生成女生数据
        HashSet<String> girlhs = new HashSet<>();
        while(true){
            if(girlhs.size() == girlCount){
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlhs.add(familyNameList.get(0) + girlNameList.get(0));
        }
        //这里将男生和女生添加到一个集合中
        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        for (String g : girlhs) {
            //随机生成年龄
            int age = r.nextInt(10) +18;
            list.add(g + "-女-" + age);
        }
        //添加男生数据
        for (String b : boyhs) {
            int age = r.nextInt(10) + 18;
            list.add(b + "-男-" + age);
        }
        Collections.shuffle(list);
        return list;
    }


    /**
     *
     * @param //girlNameStr
     * @param  //正则表达式
    */
    private static ArrayList<String> getData(String str, String regex,int index) {
        //1.创建集合存放数据
        ArrayList<String> list = new ArrayList<>();
        //2.按照正则进行获取
        Pattern pattern = Pattern.compile(regex);
        //3.获取数据
        Matcher matcher = pattern.matcher(str);
        while(matcher.find()){
            String group = matcher.group(index);
            list.add(group);
        }
        return list;

    }

    public static String webCrawler(String net) throws IOException {
        //1. 创建Stringbuilder对象拼接爬取到得数据
        StringBuilder sb = new StringBuilder();
        //2.创建URL对象
        URL url = new URL(net);
        //3.链接网站
        URLConnection conn = url.openConnection();
        //4.爬取数据
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        int ch;
        while((ch = isr.read()) != -1){
            sb.append((char)ch);
        }
        isr.close();
        return sb.toString();


    }


}
